/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package validator;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;


/**
 *
 * @author Administrator
 */
public class JRequiredValidator extends JLabel {

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    public JRequiredValidator() {
        super();
        //this.setVisible(false);
    }

    public boolean check()
    {
        Component obj = this.getLabelFor();

    	boolean isValid = true;

        if(obj instanceof JTextField)
        {
                if(((JTextField)obj).getText().equals(""))
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextArea)
        {
                if(((JTextArea)obj).getText().equals(""))
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextPane)
        {
                if(((JTextPane)obj).getText().equals(""))
                {
                        isValid = false;
                }
        }
        if(obj instanceof JCheckBox)
        {
                if(!((JCheckBox)obj).isSelected())
                {
                        isValid = false;
                }
        }
        if(obj instanceof JComboBox)
        {
                if(((JComboBox)obj).getSelectedItem() == null)
                {
                        isValid = false;
                }
        }
        
        if(!isValid)
        {
        	this.setText(ValidatorConst.VALIDATOR_REQUIRED_MESSAGE);
        	obj.setBackground(Color.GREEN);
        }
        this.setVisible(!isValid);
        
        return isValid;
    }
}
